package com.hbx.platform.auth.service.impl;

import com.hbx.platform.auth.Dto.RoleDto;
import com.hbx.platform.auth.domain.Permission;
import com.hbx.platform.auth.domain.Role;
import com.hbx.platform.auth.domain.RolePermission;
import com.hbx.platform.auth.mapper.RolePermissionMapper;
import com.hbx.platform.auth.service.IRoleService;
import com.hbx.platform.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbx
 * @since 2024-04-06
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void saveTree(RoleDto roleDto) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleDto.getRole().getId());
        List<Permission> permissions = roleDto.getPermissions();
        for (Permission permission : permissions) {
            rolePermission.setPermissionId(permission.getId());
            rolePermission.setPermissionSn(permission.getSn());
            rolePermissionMapper.save(rolePermission);
        }
    }

    @Override
    public void updateTree(RoleDto roleDto) {
        Long roleId = roleDto.getRole().getId();
        rolePermissionMapper.deleteByRoleId(roleId);
        saveTree(roleDto);
    }

    @Override
    public List<Long> getPermissionsId(Long id) {
        List<Long> ids = rolePermissionMapper.getPermissionsId(id);
        return ids;
    }
}
